package com.kk.xhr.admin.biz.impl;

import com.kk.xhr.admin.biz.INotificationBizService;
import com.kk.xhr.admin.controller.request.NotificationAddRequest;
import com.kk.xhr.admin.controller.vo.NotificationVO;
import com.kk.xhr.common.pojo.AuthUser;
import com.kk.xhr.common.pojo.BinaryEnum;
import com.kk.xhr.model.service.INotificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 通知biz服务接口实现
 *
 * @author kkmystery
 * @version 1.0 2021/3/24
 * @since 1.0.0
 */
@Service
@RequiredArgsConstructor
public class NotificationServiceImpl implements INotificationBizService {
    private final INotificationService notificationService;

    @Override
    public List<NotificationVO> query(Long userId) {
        return notificationService.query(userId)
            .stream()
            .map(dto -> NotificationVO.builder()
                .id(dto.getId())
                .message(dto.getMessage())
                .status(dto.getStatus())
                .build())
            .collect(Collectors.toList());
    }

    @Override
    public void modifyStatus(Collection<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        notificationService.modify(ids, BinaryEnum.FALSE);
    }

    @Override
    public void add(AuthUser authUser, NotificationAddRequest request) {
        String content = request.getContent() + "  from " + authUser.getUsername();
        if (authUser.getRoles().contains("admin")) {
            content += "(管理员)";
        }
        notificationService.batchAdd(request.getUserIds(), content);
    }
}
